 /*
The MIT License

Copyright (c) 2009 P.J. Onori (pj@somerandomdude.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package com.somerandomdude.colortoolkit
{
	import com.somerandomdude.colortoolkit.spaces.CMYK;
	import com.somerandomdude.colortoolkit.spaces.Gray;
	import com.somerandomdude.colortoolkit.spaces.HSB;
	import com.somerandomdude.colortoolkit.spaces.HSL;
	import com.somerandomdude.colortoolkit.spaces.Lab;
	import com.somerandomdude.colortoolkit.spaces.RGB;
	import com.somerandomdude.colortoolkit.spaces.XYZ;
	import com.somerandomdude.colortoolkit.spaces.YUV;
	
	public class ColorUtil
	{
		static private var rybWheel : Array = [[0, 0], [15, 8], [30, 17], [45, 26], [60, 34], [75, 41], [90, 48], [105, 54], [120, 60], [135, 81], [150, 103], [165, 123], [180, 138], [195, 155], [210, 171], [225, 187], [240, 204], [255, 219], [270, 234], [285, 251], [300, 267], [315, 282], [330, 298], [345, 329], [360, 0]];
		
		static public function setColorOpaque(color:int):int { return 0xff000000 | (color >> 16 & 0xff) << 16 | (color >> 8 & 0xff) << 8 | color & 0xff; }
		
		static public function desaturate(color:int):int { return new Gray(color).color; }
		
		static public function shiftBrightenessBy(color:int, degree:Number):int 
		{
			var col:HSB = new HSB();
			col.color=color;
			
			col.brightness+=degree;
			
			return col.color;
		}
		
		static public function shiftSaturation(color:int, degree:Number):int
		{
			var col:HSB = new HSB();
			col.color=color;
			
			col.saturation+=degree;
			
			return col.color;
		}
		
		static public function shiftHue(color:int, degree:Number):int
		{
			var col:HSB = new HSB();
			col.color=color;
			
			col.hue+=degree;
			
			return col.color;
		}
		
		static public function toRGB(color:int):RGB { return new Color(color).toRGB(); }
		static public function toCMYK(color:int):CMYK { return new Color(color).toCMYK(); }
		static public function toHSB(color:int):HSB { return new Color(color).toHSB(); }
		static public function toHSL(color:int):HSL { return new Color(color).toHSL(); }
		static public function toGrayscale(color:int):Gray { return new Color(color).toGrayscale(); }
		static public function toLab(color:int):Lab { return new Color(color).toLab(); }
		static public function toXYZ(color:int):XYZ { return new Color(color).toXYZ(); }
		static public function toYUV(color:int):YUV { return new Color(color).toYUV(); }
		
		static public function getComplement(color:int):int { return rybRotate(color, 180); }
		
		static public function rybRotate(color:int, angle:Number):int 
		{			
			var hsb:HSB = new HSB();
			hsb.color=color;
			
			var a : Number;
			for (var i : int = 0;i < rybWheel.length; i++) {
				var x0 : Number = rybWheel[i][0];
				var y0 : Number = rybWheel[i][1];
           
				var x1 : Number = rybWheel[i + 1][0];
				var y1 : Number = rybWheel[i + 1][1];
				if(y1 < y0)  y1 += 360;
				if(y0 <= hsb.hue && hsb.hue <= y1) {
					a = 1.0 * x0 + (x1 - x0) * (hsb.hue - y0) / (y1 - y0);
					break;
				}
			}
			
			a = (a + (angle % 360));
			if(a < 0)  a = 360 + a;
			if (a > 360)  a = a - 360;
			a = a % 360;
			
			var newHue : Number;
			for (var k : int = 0;k < rybWheel.length; k++) {
				var xx0 : Number = rybWheel[k][0];
				var yy0 : Number = rybWheel[k][1];
           
				var xx1 : Number = rybWheel[k + 1][0];
				var yy1 : Number = rybWheel[k + 1][1];
				if (yy1 < yy0) yy1 += 360;
				if (xx0 <= a && a <= xx1) {
					newHue = 1.0 * yy0 + (yy1 - yy0) * (a - xx0) / (xx1 - xx0);
					break;
				}
			}
			newHue = newHue % 360;
			hsb.hue=newHue;
			
			return hsb.color;
		}	
	}
}